<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false"
              title="我的频道">
      <van-button @click="isEdit = !isEdit"
                  class="edit-button"
                  size="small"
                  round
                  plain
                  type="danger">{{isEdit ? '完成' : '编辑'}}</van-button>
    </van-cell>
    <van-grid class="my-grid"
              :gutter="10">
      <van-grid-item @click="onMyChannel(index,item.id)"
                     :class="index===active?'active':''"
                     v-for="(item,index) in myChannels"
                     :key="item.id"
                     :text="item.name">
        <van-icon v-show="isEdit && index!==0"
                  slot="icon"
                  name="close" />
      </van-grid-item>
    </van-grid>
    <!-- 推荐频道 -->
    <van-cell :border="false"
              title="推荐频道"></van-cell>
    <van-grid class="recommend-grid"
              :gutter="10">
      <van-grid-item @click="addMyChannels(item)"
                     v-for="item in recommendChannels"
                     :key="item.id"
                     icon="plus"
                     :text="item.name" />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addMyChannels, deleteMyChannels } from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  computed: {
    // recommendChannels () {
    //   const newArr = this.allChannels.filter(item => {
    //     const findItem = this.myChannels.find(myChannelItem => {
    //       return myChannelItem.id === item.id
    //     })
    //     if (findItem) {
    //       return false
    //     } else {
    //       return true
    //     }
    //   })
    //   return newArr
    // }
    recommendChannels () {
      return this.allChannels.filter(i => !this.myChannels.find(myIt => myIt.id === i.id))
    },
    ...mapState(['user'])
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data: res } = await getAllChannels()
        this.allChannels = res.data.channels
      } catch (err) {
        this.$toast('获取数据失败')
      }
    },
    async addMyChannels (item) {
      this.myChannels.push(item)
      if (this.user) {
        try {
          await addMyChannels({
            id: item.id,
            seq: this.myChannels.length
          })
          this.$toast.success('添加成功')
        } catch (err) {
          this.$toast('添加失败')
        }
      } else {
        console.log('未登录')
        setItem('CHANNELS', this.myChannels)
      }
    },
    onMyChannel (index, id) {
      if (this.isEdit) {
        if (index === 0) return
        if (index <= this.active) {
          this.$emit('updateActive', this.active - 1, true)
        }
        this.myChannels.splice(index, 1)
        this.deleteChannelData(id)
      } else {
        this.$emit('updateActive', index, false)
      }
    },
    async deleteChannelData (id) {
      if (this.user) {
        try {
          await deleteMyChannels(id)
          this.$toast.success('删除成功')
        } catch (err) {
          this.$toast('删除失败')
        }
      } else {
        setItem('CHANNELS', this.myChannels)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.channel-edit {
  margin: 80px 15px;
  .edit-button {
    width: 104px;
    height: 48px;
    color: #f85959;
  }

  /deep/.van-grid {
    .van-grid-item {
      padding-right: 0px !important;
    }
    .van-grid-item__content {
      width: 155px;
      height: 86px;
      background: #f4f5f6;
      border-radius: 6px;
    }
    .van-grid-item__content--surround::after {
      border: none;
    }
  }

  /deep/.recommend-grid {
    .van-grid-item__content--center {
      flex-direction: row;
    }
    .van-icon {
      font-size: 25px;
    }
    .van-grid-item__icon + .van-grid-item__text {
      margin-top: 2px;
      margin-left: 2px;
    }
  }
  /deep/.my-grid {
    .van-icon-close {
      font-size: 35px;
      position: absolute;
      right: -15px;
      top: -15px;
    }
    .active {
      .van-grid-item__text {
        color: red !important;
      }
    }
    .van-grid-item__icon-wrapper {
      position: unset;
    }
  }
}
</style>
